<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:dir="http://apache.org/cocoon/directory/2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- 
    This stylesheet provides a basis for converting CoUnit 1.0 tests to CoUnit 2.0 testsuites. You
    might have to tune it to serve your needs.
    
    Authors: Jan Willem Boer <janwillem@jwenr.nl>
    -->

  <xsl:param name="base-dir"/>

  <!-- root testsuite element -->
  <xsl:template match="/dir:directory">
    <testsuite>
      <xsl:apply-templates/>
    </testsuite>
  </xsl:template>

  <!-- each directory will be treated as being a new testsuite -->
  <xsl:template match="dir:directory">
    <xsl:param name="path"/>
    <testsuite id="{@name}">
      <xsl:apply-templates>
        <xsl:with-param name="path" select="concat($path, '/', @name)"/>
      </xsl:apply-templates>
    </testsuite>
  </xsl:template>

  <!-- aggregate the files, having the xsl file as trigger -->
  <xsl:template match="dir:file[contains(@name, '.xsl')]">
    <xsl:param name="path"/>
    <!-- 
      assumption is that the testcases are built with the pattern:
      - test-<#>.xsl
      - input-<#>.xml
      - output-<#>.xml
    -->
    <!-- get the number from the filename -->
    <xsl:variable name="number" select="substring-before(substring-after(@name, 'test-'), '.xsl')"/>

    <xsl:variable name="xsl-file" select="document(concat($base-dir, $path, '/', @name))"/>

    <!-- get the id from the xsl file -->
    <xsl:variable name="id" select="$xsl-file//xsl:with-param[@name='id']/@select"/>

    <!-- get the xsl to test from the xsl file -->
    <xsl:variable name="xsl" select="$xsl-file//xsl:import[not(contains(@href, 'xsltunit'))]/@href"/>

    <!-- check if an environment is needed -->
    <xsl:variable name="environment" select="$xsl-file//xsl:variable[@name='test']/node()"/>

    <!-- for generated stylesheet input, uncomment this one -->
<!--    <xsl:variable name="generated"/>
-->    
    <xsl:variable name="generated" select="document(concat($base-dir,  $path, '/mapping-', $number,  '.xml'))"/>
    
    <!-- make the testcase -->
    <testcase id="{$number}-{$path}" description="{$id}">
      <input>
        <xsl:copy-of select="document(concat($base-dir, $path, '/input-', $number, '.xml'))"/>
      </input>
      <expect>
        <xsl:copy-of select="document(concat($base-dir, $path, '/output-', $number, '.xml'))"/>
      </expect>
      <!-- environment should be converted manually -->
      <xsl:if test="count($environment) != 0">
        <environment>
          <xsl:copy-of select="$environment"/>
        </environment>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="$generated">
          <xslt>
            <input>
              <xsl:copy-of select="$generated"/>
            </input>
            <xslt src="{$path} - ??"/>
          </xslt>          
        </xsl:when>
        <xsl:otherwise>
          <xslt src="{$base-dir}/{$xsl}"/>
        </xsl:otherwise>
      </xsl:choose>
    </testcase>

  </xsl:template>


  <!-- ignore subversion dirs -->
  <xsl:template match="dir:directory[@name='.svn']"/>

</xsl:stylesheet>
